package com.xnew.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xnew.entity.ssoApi.ResultDo;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class ApiHttp {
	
	public static ResultDo getResultDo(String url, Object object) throws Exception {
    	ResultDo resultDo = null;
		CloseableHttpClient httpClient = HttpClientBuilder.create().build();
		HttpPost httpPost = new HttpPost(url);
		System.out.println("url:" + url);
		String jsonString = JSON.toJSONString(object);
		System.out.println("jsonString:" + jsonString);
		jsonString = "[" + jsonString + "]";
		StringEntity entity = new StringEntity(jsonString, "UTF-8");
		System.out.println("entity:" + entity);
		httpPost.setEntity(entity);
		httpPost.setHeader("Content-Type", "application/json;charset=utf8");
		CloseableHttpResponse response = null;
		try {
			response = httpClient.execute(httpPost);
			System.out.println("response:" + response);
			
			HttpEntity responseEntity = response.getEntity();
			System.out.println("responseEntity:" + responseEntity);
			if (responseEntity != null) {
				System.out.println(EntityUtils.toString(responseEntity,"UTF-8"));
				resultDo = JSONObject.parseObject(EntityUtils.toString(responseEntity,"UTF-8"),ResultDo.class);
				System.out.println("resultDo--resultDo.getStatuss()" + resultDo.getStatus() + ";resultDo.getMessage():" + resultDo.getMessage() + "resultDo.getMsg():" + resultDo.getMsg() + ";resultDo.getCode()" + resultDo.getCode() + ";resultDo.getData():" + resultDo.getData());

		     }else{
		    	 return null;
		     }
		} catch (ClientProtocolException e) {
			e.printStackTrace();return null;
		} catch (ParseException e) {
			e.printStackTrace();return null;
		} catch (IOException e) {
			e.printStackTrace();return null;
		} finally {
			try {
				// 释放资源
				if (httpClient != null) {
					httpClient.close();
				}
				if (response != null) {
					response.close();
				}
			} catch (IOException e) {
				e.printStackTrace();return null;
			}
		}
    	return resultDo;
    }
	public static ResultDo getResult(String url, Object object) throws Exception {
    	ResultDo resultDo = null;

		System.out.println("url:" + url);
		String jsonString = JSON.toJSONString(object);
		System.out.println("jsonString:" + jsonString);

        //jsonString = URLEncoder.encode(jsonString, "UTF_8");
        CloseableHttpClient httpClient = HttpClients.createDefault();
        RequestConfig requestConfig = RequestConfig.custom()
                .setSocketTimeout(300 * 1000)
                .setConnectTimeout(300 * 1000)
                .build();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setConfig(requestConfig);
        httpPost.setHeader("Content-Type","application/json;charset=utf-8");
        StringEntity postingString = new StringEntity(jsonString,
                "utf-8");
        httpPost.setEntity(postingString);
		try {
			HttpResponse response = httpClient.execute(httpPost);
			System.out.println("response:" + response);
	        String content = "";
			if(response.getEntity()!=null && !response.getEntity().equals("")){
				content = EntityUtils.toString(response.getEntity(),"UTF-8");
			}
	        System.out.println("content:" + content);
			if (content != null && !content.equals("") && !content.equals("{}")) {
				resultDo = JSONObject.parseObject(content,ResultDo.class);
				System.out.println("resultDo--resultDo.getStatuss()" + resultDo.getStatus() + ";resultDo.getMessage():" + resultDo.getMessage() + "resultDo.getMsg():" + resultDo.getMsg() + ";resultDo.getCode()" + resultDo.getCode() + ";resultDo.getData():" + resultDo.getData());
		     }else{
		    	 return null;
		     }
		} catch (ClientProtocolException e) {
			e.printStackTrace();return null;
		} catch (ParseException e) {
			e.printStackTrace();return null;
		} catch (IOException e) {
			e.printStackTrace();return null;
		} finally {
			try {
				// 释放资源
				if (httpClient != null) {
					httpClient.close();
				}
			} catch (IOException e) {
				e.printStackTrace();return null;
			}
		}
    	return resultDo;
    }
}
